package com.daqian.chance.example.web.core.interceptor;

import com.daqian.chance.common.constant.WebCommonConstant;
import com.daqian.chance.core.exception.UnauthorizedException;
import com.daqian.chance.core.exception.UnregisteredException;
import com.daqian.chance.example.web.modules.member.po.entity.Member;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户登录权限拦截器
 *
 * @author: daqian
 * @date: 2019/7/26 11:06
 */
@Component
@Slf4j
public class AuthorizeInterceptor implements HandlerInterceptor {
    /**
     * 视图渲染之后的操作
     */
    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
    }

    /**
     * 处理请求完成后视图渲染之前的处理操作
     */
    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
    }

    /**
     * 进入controller层之前拦截请求
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
        log.debug("请求路径：{}", request.getServletPath());
        String tokenInHeader = request.getHeader(WebCommonConstant.X_AUTH_TOKEN);
        if (StringUtils.isEmpty(tokenInHeader)) {
            throw UnauthorizedException.msg("token不可为空");
        }
        Member member = (Member) request.getSession().getAttribute(WebCommonConstant.LOGIN_USER);
        if (member == null) {
            throw UnauthorizedException.msg("用户登录信息已失效");
        }
        // 检查用户是否绑定手机
        if (StringUtils.isEmpty(member.getPhone())) {
            throw UnregisteredException.msg("用户尚未注册");
        }
        return true;
    }

//    /**
//     * 进入controller层之前拦截请求
//     */
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
//        log.debug("请求路径：{}", request.getServletPath());
//        String tokenInHeader = request.getHeader(WebCommonConstant.TOKEN);
//        if (StringUtils.isEmpty(tokenInHeader)) {
//            throw UnauthorizedException.msg("token不可为空");
//        }
//        String tokenInSession = (String) request.getSession().getAttribute(WebCommonConstant.TOKEN);
//        if (StringUtils.isEmpty(tokenInSession)) {
//            throw UnauthorizedException.msg("token已失效");
//        }
//        // 校验token有效性
//        if (!tokenInSession.equals(tokenInHeader)) {
//            throw UnauthorizedException.msg("token不可用");
//        }
//        Member elasticSearch = (Member) request.getSession().getAttribute(WebCommonConstant.LOGIN_USER);
//        if (elasticSearch == null) {
//            throw UnauthorizedException.msg("用户登录信息已失效");
//        }
//        // 检查用户是否绑定手机
//        if (StringUtils.isEmpty(elasticSearch.getPhone())) {
//            throw UnregisteredException.msg("用户尚未注册");
//        }
//        return true;
//    }

}
